#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Niklas Hauser
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
    module.name = ":driver:pca9535"
    module.description = """\
# PCA9535 16-Bit I/O Expander

Also software-compatible with PCA9555.
The lower three address bits can be configured: `0100abc`.

Notice that you can specify multiple pins at the same time for functions
with argument type `Pins`, either by ORing the according pins, or
converting a 16-bit value using the `Pins(uint16_t)` converting constructor.

Other functions with argument type `Pin` can only take one pin.
If you want to operate on all 16-bit, use the `get(Inputs|Outputs|Directions|Polarities)()`
getters.

```cpp
using Pin = expander.Pin;
using Pins = expander.Pins;

RF_CALL_BLOCKING(expander.setOutput(Pins(0xff)); // set all lower 8 pins to output
RF_CALL_BLOCKING(expander.set(Pin::P0_0));       // set only pin 00 high
RF_CALL_BLOCKING(expander.reset(Pin::P0_1 | Pin::P0_2)); // reset 2 pins
RF_CALL_BLOCKING(expander.toggle(Pin::P0_3 | Pin::P0_4 | Pin::P0_5));    // toggle 3 pins

RF_CALL_BLOCKING(expander.readInput());  // transfer input states from the external chip
bool high = expander.read(Pin::P1_0);    // check a single pin

Pins input = expander.getInputs();   // get all 16 input states
bool isAnyPinHigh = input.any(Pin::P1_1 | Pin::P1_2 | Pin::P1_3); // check if any of 3 pins is high
```
"""

def prepare(module, options):
    module.depends(
        ":architecture:gpio.expander",
        ":architecture:i2c.device",
        ":architecture:register",
        ":processing:resumable",
        ":math:utils")
    return True

def build(env):
    env.outbasepath = "modm/src/modm/driver/gpio"
    env.copy("pca9535.hpp")
    env.copy("pca9535_impl.hpp")
